package com.multi.thread.chapter06.demo06;

import java.sql.Connection;
import java.sql.DriverManager;

public class Singleton {
	
	private enum SingletonHandler {
		connectionFactory;
		
		private Connection connection;
		
		private SingletonHandler() {
			try {
				System.out.println("调用了MyObject的构造方法");
				String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle";
				String username = "cmis";
				String password = "cmis_db";
				String driver = "oracle.jdbc.OracleDriver";
				Class.forName(driver);
				connection = DriverManager.getConnection(url, username, password);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		public Connection getConnection() {
			return connection;
		}
	}
	
	public static Connection getConnection() {
		return SingletonHandler.connectionFactory.getConnection();
	}
	
	private static class MyThread extends Thread {
		@Override
		public void run() {
			for (int i = 0; i < 2; i++) {
				System.out.println(Singleton.getConnection().hashCode());
			}
		}
	}
	
	public static void main(String[] args) {
		MyThread t1 = new MyThread();
		MyThread t2 = new MyThread();
		MyThread t3 = new MyThread();
		t1.start();
		t2.start();
		t3.start();
	}

}
